package com._jca._md5;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @Author: huangya
 * @Date: 2019/5/15
 * @Description:
 */
public class MD5Util {

//    private MessageDigest messageDigest;

    public static String toMD5(String srcStr) {
        MessageDigest messageDigest = null;
        try {
            messageDigest = MessageDigest.getInstance("md5");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        if(srcStr == null){
            throw new IllegalArgumentException("srcStr is null..");
        }
        byte[] bytes = messageDigest.digest(srcStr.getBytes());

        StringBuffer sb = new StringBuffer();
        for(int i=0; i<bytes.length; i++){
            String tmp = Integer.toHexString(bytes[i] & 0xff);
            if(tmp.length()==1){
                sb.append("0");
            }
            sb.append(tmp);
        }
        return sb.toString().toLowerCase();
    }

    public static String toMD52222(byte[] inputBytes) {
        MessageDigest messageDigest = null;
        try {
            messageDigest = MessageDigest.getInstance("md5");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        if(inputBytes == null){
            throw new IllegalArgumentException("srcStr is null..");
        }
        byte[] bytes = messageDigest.digest(inputBytes);

        StringBuffer sb = new StringBuffer();
        for(int i=0; i<bytes.length; i++){
            String tmp = Integer.toHexString(bytes[i] & 0xff);
            if(tmp.length()==1){
                sb.append("0");
            }
            sb.append(tmp);
        }
        return sb.toString().toLowerCase();
    }

    /**
     * new String("字符串", "utf-8").getBytes("utf-8")得到的值与原来不一样，
     * new String("字符串", "iso-8859-1").getBytes("iso-8859-1")不会变。
     * 但，输入流读文件时能按文件的字符集编码来解析byte。
     */
    public static String toMD5WithCharset(String srcStr, String charset) throws UnsupportedEncodingException {
        MessageDigest messageDigest = null;
        try {
            messageDigest = MessageDigest.getInstance("md5");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        if(srcStr == null){
            throw new IllegalArgumentException("srcStr is null..");
        }
        byte[] bytes = messageDigest.digest(srcStr.getBytes(charset));

        StringBuffer sb = new StringBuffer();
        for(int i=0; i<bytes.length; i++){
            String tmp = Integer.toHexString(bytes[i] & 0xff);
            if(tmp.length()==1){
                sb.append("0");
            }
            sb.append(tmp);
        }
        return sb.toString().toLowerCase();
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        System.out.println(MD5Util.toMD5("漱壕   3 \u0012\u0001 \u001Acom/_bytecode/__asm/Cwtest\u0007 \u0001\u0001 \u0010java/lang/Object\u0007 \u0003\u0001 \u0017com/_bytecode/__asm/ICw\u0007 \u0005\u0001 \u0004LESS\u0001 \u0001I\u0003\uF8F5\uF8F5\uF8F5\uF8F5\u0001 \u0005EQUAL\u0003    \u0001 \u0006GRATER\u0001 \u0012Ljava/lang/Object;\u0003   \u0001\u0001 \tcompareTo\u0001 \u0015(Ljava/lang/Object;)I\u0001 \n" +
                "ConstantValue\u0006\u0001 \u0002 \u0004 \u0001 \u0006 \u0003 \u0019 \u0007 \b \u0001 \u0011   \u0002 \t \u0019 \n" +
                " \b \u0001 \u0011   \u0002 \u000B \u0019 \f \n" +
                " \u0001 \u0011   \u0002 \u000E \u0001\u0004\u0001 \u000F \u0010    "));
    }
}
